<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        body {
            background-color: #f8f9fa;
            background-image: url('/image/backgd.jpg');
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            margin: 0;
            height: 100vh;
            overflow: hidden;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .register-container {
            width: 100%;
            max-width: 400px;
            background-color: #fff;
            padding: 10px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }
        .register-form {
            height: 400px;
            overflow-y: auto;
        }
    </style>
</head>
<body>
<div class="register-container">
    <h2 class="text-center mb-4">注册</h2>
    <div class="register-form">
        <form id="registerForm" method="post">
            <div class="form-group">
                <label for="name">姓名</label>
                <input type="text" class="form-control" id="name" name="name" required>
            </div>
            <div class="form-group">
                <label for="gender">性别</label>
                <select class="form-control" id="gender" name="gender" required>
                    <option value="0">男</option>
                    <option value="1">女</option>
                </select>
            </div>
            <div class="form-group">
                <label for="birthDate">出生日期</label>
                <input type="datetime-local" class="form-control" id="birthDate" name="birthDate" required>
            </div>
            <div class="form-group">
                <label for="idNumber">身份证号</label>
                <input type="text" class="form-control" id="idNumber" name="idNumber" required>
            </div>
            <div class="form-group">
                <label for="college">学院</label>
                <input type="text" class="form-control" id="college" name="college" required>
            </div>
            <div class="form-group">
                <label for="major">专业</label>
                <input type="text" class="form-control" id="major" name="major" required>
            </div>
            <div class="form-group">
                <label for="dormId">宿舍号</label>
                <select class="form-control" id="dormId" name="dormId" required>
                    <option value="c1">谦园1舍</option>
                    <option value="c2">谦园2舍</option>
                </select>
            </div>
            <div class="form-group">
                <label for="className">班级</label>
                <input type="text" class="form-control" id="className" name="className" required>
            </div>
            <div class="form-group">
                <label for="bedNumber">床位号</label>
                <select class="form-control" id="bedNumber" name="bedNumber" required>
                    <option value="1">1号</option>
                    <option value="2">2号</option>
                    <option value="3">3号</option>
                    <option value="4">4号</option>
                    <option value="5">5号</option>
                    <option value="6">6号</option>
                </select>
            </div>
            <div class="form-group">
                <label for="phone">电话</label>
                <input type="text" class="form-control" id="phone" name="phone" required>
            </div>
            <div class="form-group">
                <label for="email">邮箱</label>
                <input type="email" class="form-control" id="email" name="email" required>
            </div>
            <div class="form-group">
                <label for="emergencyContact">紧急联系人</label>
                <input type="text" class="form-control" id="emergencyContact" name="emergencyContact" required>
            </div>
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" class="form-control" id="username" name="username" required>
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" class="form-control" id="password" name="password" required>
            </div>
            <div class="form-group">
                <label for="origin">籍贯</label>
                <input type="text" class="form-control" id="origin" name="origin" required>
            </div>
            <button type="submit" class="btn btn-primary btn-block">注册</button>
        </form>
    </div>
    <p class="text-center mt-3">
        <a href="/login">已有账号？立即登录</a>
    </p>
</div>
</body>
<script>
    $(document).ready(function () {
        $('#registerForm').on('submit', function (event) {
            event.preventDefault();//阻止默认提交
            //转换格式
            var birthDateInput = $('#birthDate').val();
            var birthDate = new Date(birthDateInput);
            var formattedBirthDate = birthDate.toISOString().slice(0, 19).replace('T', ' ');

            var formData = {
                name: $('#name').val(),
                gender: $('#gender').val(),
                birthDate: formattedBirthDate,
                idNumber: $('#idNumber').val(),
                college: $('#college').val(),
                major: $('#major').val(),
                dormId: $('#dormId').val(),
                className: $('#className').val(),
                bedNumber: $('#bedNumber').val(),
                phone: $('#phone').val(),
                email:$('#email').val(),
                emergencyContact: $('#emergencyContact').val(),
                username: $('#username').val(),
                password: $('#password').val(),
                origin: $('#origin').val()
            };
            $.ajax({
                url:'/student/register',
                type:'POST',
                contentType:'application/json',
                data:JSON.stringify(formData),
                success:function (response) {
                    //成功
                    console.log(response);
                    if(response.code === 200){
                        alert('注册成功');
                        //重定向到其他页面
                        window.location.href = '/login';
                    }else{
                        alert('注册失败: '+response.message);
                    }
                },
                error:function (xhr,status,error){
                    alert('注册失败,请检查输入信息')
                }
            })
        })
    })
</script>
</html>
